package com.luoye.lj.platform.dao.impl;


import com.luoye.lj.platform.dao.IMuserserviceDao;
import com.luoye.lj.platform.dao.mapper.IMuserserviceMapper;
import com.luoye.lj.platform.model.domain.AuthInfo;
import com.luoye.lj.platform.model.domain.RUnitInfo;
import com.luoye.lj.platform.model.domain.RoleInfo;
import com.luoye.lj.platform.model.domain.manage.ManageLog;
import com.luoye.lj.platform.model.domain.manage.ManageLogCollection;
import com.luoye.lj.platform.model.domain.manage.ManageLogSearchCondition;
import com.luoye.lj.platform.model.domain.muser.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 15-9-29
 * Time: 上午10:11
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class MuserserviceDaoImpl implements IMuserserviceDao {
    @Autowired
    private IMuserserviceMapper muserserviceMapper;

    @Override
    public MUserInfo getUserByName(String username) {
        return muserserviceMapper.getUserByName(username);
    }

    @Override
    public RoleInfo getRoleById(long id) {
        return muserserviceMapper.getRoleById(id);
    }

    @Override
    public List<AuthInfo> getAuthsByRid(long rid) {
        return muserserviceMapper.getAuthsByRid(rid);
    }

    @Override
    public int insertUser(MUserInfo mUserInfo) {

        return muserserviceMapper.insertUser(mUserInfo);
    }

    @Override
    public List<RoleInfo> getAllRole() {
        return muserserviceMapper.getAllRole();
    }

    @Override
    public MUserInfo getUserById(long id) {
        return muserserviceMapper.getUserById(id);
    }

    @Override
    public int updateUser(MUserInfo mUserInfo) {
        return muserserviceMapper.updateUser(mUserInfo);
    }

    @Override
    public int insertLog(ManageLog manageLog) {
        return muserserviceMapper.insertLog(manageLog);
    }

    @Override
    public MUserDetailCollection searchUserDetailByCondition(MUserDetailSearchCondition muserDetailSearchCondition) {
        int totalCount=muserserviceMapper.getUserInfoCountByCondition(muserDetailSearchCondition);
        List<MUserInfo> mUserInfoList=muserserviceMapper.searchUserInfoByCondition(muserDetailSearchCondition);
        MUserDetailCollection mUserDetailCollection=new MUserDetailCollection();
        if(CollectionUtils.isEmpty(mUserInfoList)){
            return mUserDetailCollection;
        }
        List<MUserDetail> mUserDetailList=new ArrayList<MUserDetail>();
        int count=mUserInfoList.size();
        for(int i=0;i<count;i++){
            MUserInfo mUserInfo=mUserInfoList.get(i);
            if(mUserInfo!=null){
                //查询角色
                List<RoleInfo> roleInfoList=muserserviceMapper.getRolesByUid(mUserInfo.getId());
                Map<String, AuthInfo> authInfoMap = new HashMap<>();
                if(!CollectionUtils.isEmpty(roleInfoList)) {
                    //查询权限列表
                    for (RoleInfo roleInfo : roleInfoList) {
                        List<AuthInfo> authInfoListTemp = muserserviceMapper.getAuthsByRid(roleInfo.getId());
                        if(!CollectionUtils.isEmpty(authInfoListTemp)){
                            for(AuthInfo authInfo:authInfoListTemp){
                                authInfoMap.put(authInfo.getId()+"",authInfo);
                            }
                        }
                    }

                }
                MUserDetail mUserDetail=new MUserDetail();
                mUserDetail.setId(mUserInfo.getId());
                mUserDetail.setUsername(mUserInfo.getUsername());
                mUserDetail.setRealName(mUserInfo.getRealName());
                mUserDetail.setPasswd(mUserInfo.getPasswd());
                mUserDetail.setRoleInfoList(roleInfoList);
                if(!authInfoMap.isEmpty()) {
                    List<AuthInfo> authInfoList=new ArrayList<>();
                    authInfoList.addAll(authInfoMap.values());
                    mUserDetail.setAuthInfoList(authInfoList);
                }
                mUserDetail.setDefaultUrl(mUserInfo.getDefaultUrl());
                mUserDetail.setEmail(mUserInfo.getEmail());
                mUserDetail.setMobilePhone(mUserInfo.getMobilePhone());
                mUserDetail.setStatus(mUserInfo.getStatus());
                mUserDetail.setLastLoginTime(mUserInfo.getLastLoginTime());
                mUserDetailList.add(mUserDetail);
            }
        }
        mUserDetailCollection.setTotalCount(totalCount);
        mUserDetailCollection.setMuserDetailList(mUserDetailList);
        return mUserDetailCollection;
    }

    @Override
    public ManageLogCollection searchManageLogByCondition(ManageLogSearchCondition manageLogSearchCondition) {
        int totalCount=muserserviceMapper.getManageLogCountByCondition(manageLogSearchCondition);
        List<ManageLog> manageLogList=muserserviceMapper.searchManageLogByCondition(manageLogSearchCondition);
        ManageLogCollection manageLogCollection=new ManageLogCollection();
        manageLogCollection.setTotalCount(totalCount);
        manageLogCollection.setManageLogList(manageLogList);
        return manageLogCollection;
    }

    @Override
    public List<RoleInfo> getRolesByUid(long uid) {
        return muserserviceMapper.getRolesByUid(uid);
    }

    @Override
    public int addUrr(MUserSaveInfo mUserSaveInfo) {
        if(!CollectionUtils.isEmpty(mUserSaveInfo.getRidList())) {
            return muserserviceMapper.addUrr(mUserSaveInfo);
        }
        return 0;
    }

    @Override
    public int delUrr(long uid) {
        return muserserviceMapper.delUrr(uid);
    }


}
